package top.chenyaohua.utils;

import org.objectweb.asm.Type;
import top.chenyaohua.asm.AnnotationMetadata;

import java.util.List;

public class AnnotationUtils {

    /**
     * 判断是否存在指定注解
     * @param ac
     * @param name
     * @return
     */
    public static boolean isExist (List<AnnotationMetadata> ac, String name){
        AnnotationMetadata annotationMetadata = getAnnotationMetadata(ac, name);

        return annotationMetadata != null;
    }

    /**
     * 判断是否存在指定注解
     * @param ac
     * @param name
     * @return
     */
    public static AnnotationMetadata getAnnotationMetadata (List<AnnotationMetadata> ac, String name){
        if (ac == null  || name == null) {
            return null;
        }

        for (AnnotationMetadata item : ac) {

            String type = item.getAnnotationType();
            String className = Type.getType(type).getClassName();
            if(className.equals(name)){
                return item;
            }

        }

        return null;
    }
}
